home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / termSpeech.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  3KB  |  179 lines

  1. /*
  2. **    $Id: termSpeech.c,v 1.3 92/04/07 20:12:03 olsen Sta Locker: olsen $
  3. **    $Revision: 1.3 $
  4. **    $Date: 92/04/07 20:12:03 $
  5. **
  6. **    Speech support routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Local symbols. */
  15.  
  16. STATIC struct MsgPort        *NarratorPort;
  17. STATIC struct narrator_rb    *NarratorRequest;
  18. STATIC UBYTE             SpeechString[512],
  19.                  TranslatedString[512];
  20. STATIC BYTE             DidSpeak;
  21.  
  22. struct Library            *TranslatorBase;
  23.  
  24.     /* DeleteSpeech():
  25.      *
  26.      *    Free the data associated with the speech function.
  27.      */
  28.  
  29. VOID
  30. DeleteSpeech()
  31. {
  32.     if(DidSpeak)
  33.     {
  34.         ULONG Mask = 1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit;
  35.  
  36.         if(SetSignal(0,0) & Mask)
  37.         {
  38.             SetSignal(0,Mask);
  39.  
  40.             WaitIO(NarratorRequest);
  41.         }
  42.         else
  43.         {
  44.             if(!CheckIO(NarratorRequest))
  45.                 WaitIO(NarratorRequest);
  46.         }
  47.     }
  48.  
  49.     if(TranslatorBase)
  50.     {
  51.         CloseLibrary(TranslatorBase);
  52.  
  53.         TranslatorBase = NULL;
  54.     }
  55.  
  56.     if(NarratorRequest)
  57.     {
  58.         if(NarratorRequest -> message . io_Device)
  59.             CloseDevice(NarratorRequest);
  60.  
  61.         DeleteIORequest(NarratorRequest);
  62.  
  63.         NarratorRequest = NULL;
  64.     }
  65.  
  66.     if(NarratorPort)
  67.     {
  68.         DeleteMsgPort(NarratorPort);
  69.  
  70.         NarratorPort = NULL;
  71.     }
  72.  
  73.     DidSpeak = FALSE;
  74. }
  75.  
  76.     /* CreateSpeech():
  77.      *
  78.      *    Open translator.library and narrator.device, perform
  79.      *    the necessary setups for the speech function.
  80.      */
  81.  
  82. BYTE
  83. CreateSpeech()
  84. {
  85.     if(TranslatorBase = OpenLibrary("translator.library",0))
  86.     {
  87.         if(NarratorPort = CreateMsgPort())
  88.         {
  89.             if(NarratorRequest = (struct narrator_rb *)CreateIORequest(NarratorPort,sizeof(struct narrator_rb)))
  90.             {
  91.                     /* Any channel will do. */
  92.  
  93.                 NarratorRequest -> ch_masks        = &AnyChannel[0];
  94.                 NarratorRequest -> nm_masks        = 4;
  95.  
  96.                     /* This is a write request. */
  97.  
  98.                 NarratorRequest -> message . io_Command    = CMD_WRITE;
  99.                 NarratorRequest -> message . io_Data    = (APTR)SpeechString;
  100.  
  101.                 if(!OpenDevice("narrator.device",0,NarratorRequest,0))
  102.                 {
  103.                     SpeechSetup();
  104.  
  105.                     return(TRUE);
  106.                 }
  107.             }
  108.         }
  109.     }
  110.  
  111.     DeleteSpeech();
  112.  
  113.     return(FALSE);
  114. }
  115.  
  116.     /* Say(UBYTE *String,...):
  117.      *
  118.      *    Translate a string into phonemes and speak it.
  119.      */
  120.  
  121. VOID
  122. Say(UBYTE *String,...)
  123. {
  124.     if(TranslatorBase && SpeechConfig . Enabled && English)
  125.     {
  126.         va_list VarArgs;
  127.  
  128.         if(DidSpeak)
  129.         {
  130.             ULONG Mask = 1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit;
  131.  
  132.             if(SetSignal(0,0) & Mask)
  133.             {
  134.                 SetSignal(0,Mask);
  135.  
  136.                 WaitIO(NarratorRequest);
  137.             }
  138.             else
  139.             {
  140.                 if(!CheckIO(NarratorRequest))
  141.                     WaitIO(NarratorRequest);
  142.             }
  143.         }
  144.  
  145.         va_start(VarArgs,String);
  146.         VSPrintf(TranslatedString,String,VarArgs);
  147.         va_end(VarArgs);
  148.  
  149.         if(!Translate(TranslatedString,strlen(TranslatedString),SpeechString,511))
  150.         {
  151.             NarratorRequest -> message . io_Length = strlen(SpeechString);
  152.  
  153.             SetSignal(0,1 << NarratorRequest -> message . io_Message . mn_ReplyPort -> mp_SigBit);
  154.  
  155.             SendIO(NarratorRequest);
  156.  
  157.             DidSpeak = TRUE;
  158.         }
  159.     }
  160. }
  161.  
  162.     /* SpeechSetup():
  163.      *
  164.      *    Transfer the configuration data into the setup.
  165.      */
  166.  
  167. VOID
  168. SpeechSetup()
  169. {
  170.     if(NarratorRequest)
  171.     {
  172.         NarratorRequest -> rate        = SpeechConfig . Rate;
  173.         NarratorRequest -> pitch    = SpeechConfig . Pitch;
  174.         NarratorRequest -> sex        = SpeechConfig . Sex;
  175.         NarratorRequest -> volume    = SpeechConfig . Volume;
  176.         NarratorRequest -> sampfreq    = SpeechConfig . Frequency;
  177.     }
  178. }
  179.